//
//  DockLocationButton.m
//  团购
//
//  Created by 樊荣海 on 15/1/20.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "DockLocationButton.h"
#define imageScale 0.5

@implementation DockLocationButton {

    UIPopoverController *_popover;
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self == [super initWithFrame:frame]) {
        
        [self setImage:@"ic_district" selectedImage:@"ic_district_hl"];
        
        //底部固定, 自动调节与"顶部"的间距
        self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        
        self.imageView.contentMode = UIViewContentModeCenter;
        [self setTitle:@"定位中" forState:UIControlStateNormal];
        [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        [self addTarget:self action:@selector(locationClick) forControlEvents:UIControlEventTouchDown];
    }
    
    
    return self;
}

- (void)locationClick {

    self.enabled = NO;
    
    CityViewController *city = [[CityViewController alloc] init];
    
    //[重要]popoverController
    _popover = [[UIPopoverController alloc] initWithContentViewController:city];
    _popover.popoverContentSize = CGSizeMake(320, 480);
    _popover.delegate = self;
    
    //self.frame ,  self.superView
    [_popover presentPopoverFromRect:self.bounds inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    //[重要]监听屏幕旋转--先移除再添加
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenRotate) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    
    // 6.监听城市改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cityChange:) name:kCityChangeNote object:nil];
}

- (void)screenRotate {

    NSLog(@"屏幕发生了旋转");

    //暂时不需要
//    if ([_popover isPopoverVisible]) {
//        [_popover dismissPopoverAnimated:YES];
       //旋转1秒后再显示
//       [self performSelector:@selector(locationClick) withObject:nil afterDelay:1.0];
//    }
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {

    NSLog(@"popover被销毁, 移除通知");
    
    self.enabled = YES;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark 城市改变
- (void)cityChange:(NSNotification *)sender
{
    CityModel *city = (CityModel *)sender.object;
    
    // 1.更改显示的城市名称
    [self setTitle:city.name forState:UIControlStateNormal];
    
    // 2.关闭popover（代码关闭popover不会触发代理方法）
    [_popover dismissPopoverAnimated:YES];
    
    // 3.变为enable
    self.enabled = YES;
    
//    // 4.移除圈圈
//    [_indicator removeFromSuperview];
//    _indicator = nil;
    
    // 5.设置图标
    [self setImage:@"ic_district.png" selectedImage:@"ic_district_hl.png"];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect {

    return CGRectMake(0, 10, contentRect.size.width, contentRect.size.height*imageScale);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect {

    return CGRectMake(0, contentRect.size.height*imageScale, contentRect.size.width, contentRect.size.height*(1-imageScale));
}


@end
